package com.Beans.Dao;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.Beans.Models.Area;
import com.Beans.Dao.HibernateUtil;

public class AreaDAO {
	
	public static Area findById(int id){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tr = null;
		Area a = null;
		try{
			tr = session.beginTransaction();
			Query q = session.createQuery("FROM Area a WHERE a.id= :id");
			q.setParameter("id", id);
			a = (Area)q.uniqueResult();
			tr.commit();
		}catch(HibernateException e){
			tr.rollback();
			e.printStackTrace();
		}finally{
			session.close();
		}
		return a;
	}
	
	public static List<Area> findAll(){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tr = null;
		List <Area> a = null;
		try{
			tr = session.beginTransaction();
			Query q = session.createQuery("FROM Area");
			a = (List<Area>)q.list();
			tr.commit();
		}catch(HibernateException e){
			tr.rollback();
			e.printStackTrace();
		}finally{
			session.close();
		}
		return a;
		
	}
	
	public static Area findByArea(String area_nombre){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tr = null;
		Area a = null;
		try{
			tr = session.beginTransaction();
			Query q = session.createQuery("FROM Area a WHERE a.nombre = :area_nombre");
			q.setParameter("area_nombre", area_nombre);
			a = (Area) q.uniqueResult();
			tr.commit();
		}catch(HibernateException e){
			tr.rollback();
			e.printStackTrace();
		}finally{
			session.close();
		}
		return a;
	}
}
